<?php
/**
 * 商品管理
 * author:ty
 * date:2017-1-15
 */
namespace App\Http\Controllers;

use App\Http\Models\Fitment;
use Illuminate\Support\Facades\Input;

class FitmentController extends Controller
{
    protected $web_img = null;
    protected $erp_img = null;
    public function __construct()
    {
        $this->web_img=env('APP_WEB_PATH');
        $this->erp_img=env('STATIC_FILE_ERP_PATH');
    }

    /**家具列表页展示*/
    public function fitment_list()
    {
        /**
         * 参数整理
         * @request 请求参数
         * @value 参数值
         * @isnumber 是否是number类型
         * @empty 是否必填
        */
        $request = array(
            'fitment_name' => array('value'=>Input::get('fitment_name'),'isnumber'=>false,'empty'=>false,'declare'=>'家具名'),
            'fitment_brand' => array('value'=>Input::get('fitment_brand'),'isnumber'=>true,'empty'=>false,'declare'=>'品牌id'),
            'fitment_rent' => array('value'=>Input::get('fitment_rent',1),'isnumber'=>true,'empty'=>false,'declare'=>'租赁方式type'),//【1,2】
            'fitment_type_classify' => array('value'=>Input::get('fitment_type_classify'),'isnumber'=>true,'empty'=>false,'declare'=>'家具分类id'),
            'fitment_subcate_id' => array('value'=>Input::get('fitment_subcate_id'),'isnumber'=>true,'empty'=>false,'declare'=>'家具分类下子分类id'),
            'minprice' => array('value'=>Input::get('minprice'),'isnumber'=>true,'empty'=>false,'declare'=>'最低价格'),
            'maxprice' => array('value'=>Input::get('maxprice'),'isnumber'=>true,'empty'=>false,'declare'=>'最高价格'),
            'fitment_orderby' => array('value'=>Input::get('fitment_orderby',1),'isnumber'=>true,'empty'=>false,'declare'=>'排序规则'),
            'current_page' => array('value'=>Input::get('current_page',1),'isnumber'=>true,'empty'=>false,'declare'=>'当前分页码'),
            'per_page' => array('value'=>Input::get('per_page'),'isnumber'=>true,'empty'=>true,'declare'=>'分页长度')
        );
        //请求参数校验及处理
        $param= array();
        foreach ($request as $key=>$val) {
            if($val['empty']){
                if(empty($val['value'])){
                    $result = ['code'=>'1004','messages'=>$key.$val['declare'].'为必填参数'];
                    return response()->json($result);
                }
                if($val['isnumber'] && !is_numeric($val['value'])){
                    $result = ['code'=>'1004','messages'=>$key.$val['declare'].'必须是数字'];
                    return response()->json($result);
                }
            }
            $param[$key]=$val['value'];
        }

        $data = Fitment::fitment_list($param);

        $result = ['code'=>200,'messages'=>'商品列表数据','result'=>$data];

        return response()->json($result);
    }

    /**家具详情*/
    public function fitment_detail()
    {
        $param['id']=Input::get('id','');
        $param['rent_type']=Input::get('rent_type',1);
        if(empty($param['id']) || $param['id']<1){
            $result = ['code'=>'1004','messages'=>'请求参数(家具详情id)id必须是数字不能为空'];
            return response()->json($result);
        }
        $data = Fitment::fitment_detail($param);
        preg_match_all('/<img.*?src="(.*?)".*?>/is',$data['0']->fitments_contents,$match);
        $data['0']->fitments_contents=$match[1][0];

        $result=['code'=>200,'messages'=>'商品详情表数据','result'=>$data];

        return response()->json($result);
    }

    /**首页家具分类(测试使用，已弃用)*/
    public function fitment_classify()
    {
        $request = array(
            'fitments_type' => array('value'=>Input::get('fitments_type'),'isnumber'=>true,'empty'=>false,'declare'=>'家具分类id'),
            'fitments_subcate_id' => array('value'=>Input::get('fitments_subcate_id'),'isnumber'=>true,'empty'=>false,'declare'=>'家具分类子分类id'),
            'rent_type' => array('value'=>Input::get('rent_type','2'),'isnumber'=>true,'empty'=>false,'declare'=>'租赁方式'),
            'current_page' => array('value'=>Input::get('current_page',1),'isnumber'=>true,'empty'=>false,'declare'=>'当前页码'),
            'per_page' => array('value'=>Input::get('per_page'),'isnumber'=>true,'empty'=>true,'declare'=>'分页长度')
        );
        $param= array();
        foreach ($request as $key=>$val) {
            if($val['empty']){
                if(empty($val['value'])){
                    $result = ['code'=>'1004','messages'=>$key.$val['declare'].'为必填参数'];
                    return response()->json($result);
                }
                if($val['isnumber'] && !is_numeric($val['value'])){
                    $result = ['code'=>'1002','messages'=>$key.$val['declare'].'必须是数字'];
                     return response()->json($result);
                }
            }
            $param[$key]=$val['value'];
        }
        $data = Fitment::fitment_classify($param);
        $result = ['code'=>200,'messages'=>'家具分类','result'=>$data];
        return response()->json($result);
    }

    /**家具分类名*/
    public function cate()
    {
        $data = Fitment::cate();
        $result = ['code'=>200,'messages'=>'家具分类','result'=>$data];
        return response()->json($result);
    }


    /**
     * 家具分类子分类
     * @id 必填参数
     * @pid 家具类型id【1办公，2商业，3居家，14热门】
     */
    public function fitment_subcate_list()
    {
        $param['id']=Input::get('id');
        if(empty($param['id']) || !is_numeric($param['id'])){
            $result = ['code'=>'1002','messages'=>'请求参数id家具id必须是数字且不能为空'];
            return response()->json($result);
        }

        $data = Fitment::fitment_subcate_list($param);
        $result = ['code'=>200,'messages'=>'家具子分类数据','result'=>$data];
        return response()->json($result);
    }

}
